//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    screenHW: null,
    playerSrc: null,
    channel: '',
    hotList: '',
    tabshow: 1,
    hotrequest: false,
    channelAct: null,
    playing: false,
    autoPlay: false,
    landscape: false,
    wifiList: '',
    redFlyRain: false,
    walletView: 2,
    fullModal: {
      show: false,
      content: ''
    },
    halfModal: {
      show: false,
      scroll: false,
      content: ""
    },
    redPack: {
      "list": [],
      "code": 'ok',
      "userid": "ACB",
      "token": "sdh1u32dsada"
    },
    myWallet: {
      "money": {
        "balance": 115.42 //账户余额
      },
      "coupons": {
        "list": [{
          "id": "hdahd12313",
          "type": "coupons",
          "name": "优惠券",
          "deduction": 50, // 满减券 减多少金额
          "condition": 500, // 满多少才减
          "validity_date": 1516584360, //有效时间开始
          "expires_date": 1517755140, //过期时间
          "token": "sdadadsdas" // 【0130新增】兑奖信息
        },
        {
          "id": "hdahd1sdasd13",
          "type": "coupons",
          "name": "优惠券",
          "deduction": 100, // 满减券 减多少金额
          "condition": 1000, // 满多少才减
          "validity_date": 1516584360, //有效时间开始
          "expires_date": 1517755140, //过期时间
          "token": "hsdkfjlkdfj" // 【0130新增】兑奖信息
        },
        {
          "id": "hdahd12313",
          "type": "gift",
          "name": "188酒卡",
          "deduction": 188, // 现金券面值
          "condition": null, // 使用条件
          "validity_date": 1516584360,
          "expires_date": 1517755140,
          "token": "asdsadwqe" // 【0130新增】兑奖信息
        },
        {
          "id": "hdahd1sdasd13",
          "type": "coupons",
          "name": "优惠券",
          "deduction": 100, // 满减券 减多少金额
          "condition": 1000, // 满多少才减
          "validity_date": 1516584360, //有效时间开始
          "expires_date": 1517755140, //过期时间
          "token": "sadasdwqwewe" // 【0130新增】兑奖信息
        },
        {
          "id": "hdahd12313",
          "type": "gift",
          "name": "188酒卡",
          "deduction": 188, // 现金券面值
          "condition": null, // 使用条件
          "validity_date": 1516584360,
          "expires_date": 1517755140,
          "token": "eqwdfdsgsdg" // 【0130新增】兑奖信息
        },
        {
          "id": "hdahd1sdasd13",
          "type": "coupons",
          "name": "优惠券",
          "deduction": 100, // 满减券 减多少金额
          "condition": 1000, // 满多少才减
          "validity_date": 1516584360, //有效时间开始
          "expires_date": 1517755140, //过期时间
          "token": "wqeqrewrwer" // 【0130新增】兑奖信息
        },
        {
          "id": "hdahd12313",
          "type": "gift",
          "name": "188酒卡",
          "deduction": 188, // 现金券面值
          "condition": null, // 使用条件
          "validity_date": 1516584360,
          "expires_date": 1517755140,
          "token": "sdsafrterer" // 【0130新增】兑奖信息
        }]
      },
      "card": {
        "series": [{  // 集卡系列
          "series_id": "jshdah132312",
          "series_name": "汪年新春祝福", // 系列名称
          "sort": 0, // 系列显示顺序
          "conditions": true, // 是否满足兑换条件
          "list": [{ // 【0130 - 上午增补】 每个系列暂定9张， 8 + 1集齐为特等奖
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/01.jpg", // 卡的图片地址
            "own": true  // 用户是否拥有此卡
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/02.jpg",
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/03.jpg",
            "own": true
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/04.jpg",
            "own": true
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/05.jpg",
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/06.jpg",
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/07.jpg",
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/08.jpg",
            "own": false
          },
          {
            "special": true, 
            "card_id": "weqdfqwe12",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_dog/09.jpg",
            "own": true
          }]
        },
        {
          "series_id": "jshdah132312",
          "series_name": "「剧」在一起过新年",
          "sort": 1,
          "conditions": true,
          "list": [{
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/01.jpg", 
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/02.jpg",
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/03.jpg",
            "own": true
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/04.jpg",
            "own": true
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/05.jpg",
            "own": true
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/06.jpg",
            "own": false
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/07.jpg",
            "own": true
          },
          {
            "special": false,
            "card_id": "2sdadsdad2",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/08.jpg",
            "own": false
          },
          {
            "special": true,
            "card_id": "weqdfqwe12",
            "img_src": "https://vodcdn.southtv.cn/webtv/agreement/mina/newyear_bless/09.jpg",
            "own": false
          }]
        }]
      }
    }
  },
  onReady: function (res) {
    var that = this
    wx.request({
      url: 'http://new.southtv.cn:9180/api/querylive',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let _channel = res.data.result
        for (let i = 0; i < _channel.length; i++) {
          let
            _host = res.data.play_host,
            _path = _channel[i].playpath,
            _type = ".m3u8?type=live",
            _imgtype = ".jpg?type=live&thumbnail=thumbnail_small.jpg",
            _postertype = ".jpg?type=live";

          _channel[i].wxplay = _host + _path + _type
          _channel[i].wximg = _host + _path + _imgtype
          _channel[i].wxposter = _host + _path + _postertype
        }
        that.setData({
          channel: _channel,
          // playerSrc: [_channel[0].wxplay, _channel[0].wxposter],
          playerSrc: ["http://live-ws-hls.huomaotv.cn/live/jade/playlist.m3u8", _channel[0].wxposter],
          channelAct: _channel[0].id
        });
      }
    })
    //红包雨
    // this.redRain()
    
    // player fullscreen
    this.videoContext = wx.createVideoContext('nxplayer')
    var that = this
    wx.onAccelerometerChange(function (res) {
      let 
        _flatwise = ( res.z < -0.7 || res.z > 0.7 ),
        _void = ( res.y >= 0.4 && res.y <= 0.3)

      if (_flatwise || _void){
        return true;
      }else if (res.x < -0.6) {
        that.videoContext.requestFullScreen()
        that.data.landscape = true
        that.lazyScreen()
      } else if (that.data.landscape) {
        that.videoContext.exitFullScreen()
        that.data.landscape = false
        that.lazyScreen()
      }
    })
  },
  onLoad: function () {
    var that = this
    var _model
    wx.getSystemInfo({
      success: function (res) {
        _model = res.model
        that.data.screenHW = {  
          height: res.screenHeight,
          width: res.screenWidth,  
        }
      }
    })
    if (_model == 'OD105a') {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          var _lat = res.latitude
          var _lon = res.longitude
          var locRadio = (_lat > 23.1360239661 && _lat < 23.1384312401 && _lon > 113.2571715117 && _lon < 113.2599127293)
          if (locRadio) {
            wx.showToast({
              title: '你在公司附近'
            })
          }
        }
      })
    }
  },
  lazyScreen() {
    wx.stopAccelerometer()
    setTimeout(() => {
      wx.startAccelerometer()
    }, 2000)
  },
  randomInt(min, max) {
    return parseInt(Math.random() * (max - min) + min);
  },
  randomType() {
    let _type = this.randomInt(0,3)
    switch (_type) {
      case 1:
        return "现金"
        break;
      case 2:
        return "商城券"
        break;
      case 3:
        return "酒卡"
        break;
      default:
        return "没有抢到喔"
    }
  },
  playerPlay() {
    this.data.playing = true
  },
  playerPause() {
    this.data.playing = false
  },
  changePath: function (event) {
    if (!this.data.autoPlay) {
      this.setData({ autoPlay: true })
    }
    let _data = event.currentTarget.dataset
    this.setData({ 
      playerSrc: [_data.src, _data.poster],
      channelAct: _data.id  
    })
  },
  tabLive: function () {
    this.setData({ tabshow: 1 })
  },
  tabMe: function () {
    this.setData({ tabshow: 3 })
  },
  tabHot: function () {
    if (!this.data.hotrequest){
      var that = this
      wx.request({
        url: 'https://apicdn.southtv.cn/api?action=listVideos&cid=all&details=1&page_no=1&page_size=10&sort=monhot asc&sort=created_time desc&type=video&uid=53E4C3A35B2730F44FBE09EBD3A2EC07&referer=https://apicdn.southtv.cn',
        headers: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          // console.log(res.data.result)
          let _hotList = res.data.result
          for (let i = 0; i < _hotList.length; i++) {
            let
              _host = res.data.play_host,
              _path = _hotList[i].playpath,
              _type = ".m3u8?type=vod",
              _postertype = ".jpg?type=vod";

            _hotList[i].wxplay = _host + _path + _type
            _hotList[i].wximg = _host + _path + _postertype
            _hotList[i].wxposter = _host + _path + _postertype
          }
          that.setData({ hotList: res.data.result });
        }
      })
      this.data.hotrequest = true
    }
    this.setData({ tabshow: 2 })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') { console.log(res.target) }
    return {
      title: '手机看电视新姿势，南方无线邀您一起炫！',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '小程序分享成功',
        })
      },
      fail: function (res) {}
    }
  },
  walletChange(event) {
    this.setData({ walletView: event.currentTarget.dataset.view })    
  },
  redRain() {
    // redpack
    let _array = [], _obj = {}, _left, _delay,
      _alldelay = 2000, //红包雨开始延时
      _endseed = 500,
      _spacetimes = 20,
      _raintime = _endseed * _spacetimes + _alldelay + 3000, //红包雨下落时间
      _num = 30
    for (let i = 0; i < _num; i++) {
      _obj = {}
      _obj.id = i
      _obj.position = this.randomInt(0, 9) * 10
      _obj.delay = this.randomInt(0, _endseed) * _spacetimes + _alldelay
      _obj.type = this.randomType()
      _array.push(_obj)
      // console.log(_obj)
    }
    let _list = "redPack.list"
    this.setData({
      redFlyRain: true,
      [_list]: _array
    })
    setTimeout(() => {
      this.setData({
        redFlyRain: false,
        [_list]: ''
      })
    }, _raintime)
  },
  grabRed(event) {
    let 
      _type = '抢到' + event.currentTarget.dataset.type
    wx.showToast({
      title: _type
    })
  },
  topRedClick() {
    this.setData({
      halfModal: {
        "show": true,
        "scroll": true,
        "type": "topRed",
        "content": "https://vodcdn.southtv.cn/webtv/mina/test/redTest-0202.jpg"
      }
    })
  },
  modaClose() {
    this.setData({
      fullModal: {
        show: false
      }
    })
  },
  halfModaClose() {
    let animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })

    this.animation = animation

    animation.scale(2, 2).rotate(45).step()

    this.setData({
      halfModal: {
        show: false,
        ani: animation.export()
      }
    })
  },
  // halfModaMoveEvent(e) {
  //   let 
  //     touchs = e.touches[0],
  //     pageX = touchs.pageX,
  //     pageY = touchs.pageY,
  //     scHeight = this.data.screenHW.height,
  //     scWidth = this.data.screenHW.width,
  //     hModaHeight = scWidth * 0.7 + 40,
  //     _bottom = -(pageY - hModaHeight)

  //   this.setData({ halfModaBottom: _bottom })
  //   if (pageY - hModaHeight > 100 ){
  //     this.halfModaClose()
  //     this.setData({ halfModaBottom: 0 })
  //   }
  // },
  cponClick(event) {
    let
      that = this,
      _item = event.currentTarget.dataset.item
    wx.setClipboardData({
      data: _item.token,
      success: function (res) {
        _item.clipboard = true
        that.cponOpen(_item)
      },
      fail: function (res) {
        _item.clipboard = false
        that.cponOpen(_item)
      }
    })
  },
  cponOpen(_item) {
    this.setData({
      halfModal: {
        show: true,
        type: "cpon",
        content: _item
      }
    })
  },
  cardClick(event) {
    let
      _item = event.currentTarget.dataset.item
    if(_item.own){
      console.log(_item)
      this.setData({
        halfModal: {
          show: true,
          bg: "linear-gradient(60deg, #c3aef1, #e5bbca)",
          type: "card",
          content: _item.img_src
        }
      })
    }
  },
  toWithdraw() {
    this.setData({
      halfModal: {
        show: true,
        bg: "#f5f5f5",
        type: "withdraw",
        content: ''
      }
    })    
  }
})
